package com.angel.controller;

import com.angel.bean.AcupuncturePoint;
import com.angel.bean.ResourceCombination;
import com.angel.bean.ResourceCombination;
import com.angel.bean.Webresource;
import com.angel.exception.StatusException;
import com.angel.service.*;
import com.angel.service.ResourceCombinationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/resourceCombinationController")
public class ResourceCombinationController {

    @Resource
    private ResourceCombinationService resourceCombinationService;
    @Resource
    private AcupuncturePointService acupuncturePointService;
    @Resource
    private WebresourceService webresourceService;
    @Autowired
    private OtherlogService otherlogService;


    /**
     * 删除
     *
     * @param resourceCombination
     * @return
     */
    @RequestMapping("/delResourceCombination")
    @ResponseBody
    public Object delResourceCombination(ResourceCombination resourceCombination) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = resourceCombinationService.delete(resourceCombination);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 添加
     *
     * @param resourceCombination
     * @return
     */
    @RequestMapping("/addResourceCombination")
    @ResponseBody
    public Object addResourceCombination(ResourceCombination resourceCombination) {
        resourceCombination.setReId(resourceCombination.getReId().replaceAll(" ",""));
        resourceCombination.setState(1);
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = resourceCombinationService.insert(resourceCombination);
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 更新
     *
     * @param resourceCombination
     * @return
     */
    @RequestMapping("/updateResourceCombination")
    @ResponseBody
    public Object updateResourceCombination(ResourceCombination resourceCombination) {
        resourceCombination.setReId(resourceCombination.getReId().replaceAll(" ",""));
        int result = resourceCombinationService.update(resourceCombination);
        Map<Object, Object> map = new HashMap<Object, Object>();
        if (result != 0) {
            map.put("result", "success");
        }

        return map;
    }

    /**
     * 查询单个
     *
     * @param resourceCombination
     * @return
     */
    @RequestMapping("/findOneResourceCombination")
    @ResponseBody
    public Object findOneResourceCombination(ResourceCombination resourceCombination) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        resourceCombination.setState(1);
        ResourceCombination webres = resourceCombinationService.findOne(resourceCombination);
        if (webres != null) {
            map.put("webres", webres);
        }

        return map;
    }

    /**
     * 查询所有资源
     *
     * @param model
     * @return list
     */
    @RequestMapping("/findResourceCombination")
    @ExceptionHandler(value = StatusException.class)
    public String findResourceCombination(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {

        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5);
        ResourceCombination resourceCombination = new ResourceCombination();
        resourceCombination.setState(1);

        List<ResourceCombination> resourceCombinationList = resourceCombinationService.findAll(resourceCombination);

        PageInfo pageInfo = new PageInfo(resourceCombinationList);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "ResourceCombinationEntry";
    }


    /**
     * 查询分店信息
     *
     * @return
     */
    @RequestMapping("/findAcupuncturePoint")
    @ResponseBody
    public Object findAcupuncturePoint() {
        AcupuncturePoint acupuncturePoint = new AcupuncturePoint();
        acupuncturePoint.setState(1);
        Map<Object, Object> map = new HashMap<Object, Object>();
        List<AcupuncturePoint> acupuncturePointList = acupuncturePointService.findAll(null);
        if (acupuncturePointList != null) {
            map.put("acupuncturePointList", acupuncturePointList);
        }
        return map;
    }

    /**
     * 查询所有视频资源（排除图片）
     *
     * @return
     */
    @RequestMapping("/findWebresource")
    @ResponseBody
    public Object findWebresource() {
        Webresource webresource = new Webresource();
        webresource.setRetype("视频");
        webresource.setState(1);
        Map<Object, Object> map = new HashMap<Object, Object>();
        List<Webresource> webresourceList = webresourceService.findWebresource(webresource);
        if (webresourceList != null) {
            map.put("webresourceList", webresourceList);
        }
        return map;
    }

    /**
     * 根据id获得资源，穴位
     *
     * @param resid
     * @param acupid
     * @return
     */
    @RequestMapping("/getNameResourceCombination")
    @ResponseBody
    public Object getNameResourceCombination(@RequestParam(value = "resid") int resid,
                                             @RequestParam(value = "acupid") int acupid) {
        Webresource webresource = new Webresource();
        webresource.setReid(resid);
        Map<Object, Object> map = new HashMap<Object, Object>();
        Webresource webres = webresourceService.findOne(webresource);
        AcupuncturePoint acupuncturePoint = new AcupuncturePoint();
        acupuncturePoint.setAcupId(acupid);
        AcupuncturePoint acup = acupuncturePointService.findOne(acupuncturePoint);

        if (webres != null && acup != null) {
            map.put("webres", webres);
            map.put("acup", acup);
        }
        return map;
    }
}
